
import wx from 'weixin-js-sdk'
import router from "@/router";
import { Toast } from 'mint-ui';
import 'mint-ui/lib/style.css'

//调用微信支付
export function jsSDK (params,payWay) {
   if (typeof window.WeixinJSBridge === 'undefined') {
     if (document.addEventListener) {
      document.addEventListener('WeixinJSBridgeReady', function () { onBridgeReady(params,payWay) }, false)
    } else if (document.attachEvent) {
      document.attachEvent('WeixinJSBridgeReady', function () { onBridgeReady(params,payWay) })
      document.attachEvent('onWeixinJSBridgeReady', function () { onBridgeReady(params,payWay) })
    }
  } else {
  	console.log(payWay)
    onBridgeReady(params,payWay)
  }
}
         
function onBridgeReady(params, payWay) {
	window.WeixinJSBridge.invoke(
		'getBrandWCPayRequest', {
			'appId': params.data.appId, // 公众号名称，由商户传入
			'timeStamp': params.data.timeStamp, // 时间戳，自1970年以来的秒数
			'nonceStr': params.data.nonceStr, // 随机串
			'package': params.data.packageValue,
			'signType': params.data.signType, // 微信签名方式：
			'paySign': params.data.paySign // 微信签名
		},
		function(res) {
			if(payWay == "租赁") {
				console.log(res)
				if(res.err_msg == "get_brand_wcpay_request:ok") {
					router.push({
						path: "/waitpickgood",
						query: {
							interface_url: '/customer/orders',
							statu:1,
							style_name: '取货码',
							order_title: '待取货',
							style_title:'实付款'
						}
					});
				} else {
					console.log(res)
					router.push({
						path: "/waitpayments",
						query: {
							interface_url: '/customer/orders',
							statu:0,
							style_name: '付款',
							order_title: '待付款',
							style_title:'需付款'
						}
					});
				}
			}
			if(payWay == "付款") {
				console.log(res)
				if(res.err_msg == "get_brand_wcpay_request:ok") {
					console.log(res)
					console.log(payWay)
					router.push({
						path: "/waitpickgood",
						query: {
							interface_url: '/customer/orders',
							statu:1,
							style_name: '取货码',
							order_title: '待取货',
							style_title:'实付款'
						}
					});
				} else if(res.err_msg === 'get_brand_wcpay_request:cancel') {
					Toast("支付取消");
				}else{
					console.log(res)
					Toast("支付失败");
				}
			}
			if(payWay == "结算"){
				console.log(res)
				if(res.err_msg == "get_brand_wcpay_request:ok") {
					router.push({
						path: "/completeorder",
						query: {
							interface_url: '/customer/orders',
							statu:4,
							styletitle:'实付款',
							order_time:'还货时间',
							order_title:'结算订单',
						}
					});
				} else if(res.err_msg === 'get_brand_wcpay_request:cancel') {
					Toast("支付取消");
				}else{
					console.log(res)
					Toast("支付失败");
				}
			}
	     if(payWay == "充值") {
				if(res.err_msg == "get_brand_wcpay_request:ok") {
					router.push("/myselfaccount");
				} else if(res.err_msg === 'get_brand_wcpay_request:cancel') {
					Toast("支付取消");
				}else{
					console.log(res)
					Toast("支付失败");
				}
	     }
		 if(payWay == "预付") {
				if(res.err_msg == "get_brand_wcpay_request:ok") {
					router.push({
						path: "/completeorder",
						query: {
							interface_url: '/customer/orders',
							statu:2,
							styletitle:'实付款',
							order_time:'取货时间',
							order_title:'我的租赁',
						}
					});
				} else if(res.err_msg === 'get_brand_wcpay_request:cancel') {
					Toast("支付取消");
				}else{
					console.log(res)
					Toast("支付失败");
				}
	     }
		
		
		
						
		}
	)
}